$j(document).ready(function () {
});

//显示 隐藏全部
function toggleScreenAll(linkDOM){
	if($j(linkDOM).hasClass("expand")){
		$j(linkDOM).removeClass("expand").addClass("shrink");
		$j('span.toggleScreen').each(function(){
			var jTrThis = $j(this).parents('tr:first');
			var jTrFull = jTrThis.next();
			var consultId = jTrThis.attr("consultId");
			jTrFull.show();
			$j(this).removeClass("expand").addClass("shrink");
			if($j('#readStatus_'+consultId).hasClass("unRead")){
				//未读状态
				markRead(consultId);
			}
		});
	
	}else{
		$j(linkDOM).removeClass("shrink").addClass("expand");
		$j('span.toggleScreen').each(function(){
			var jTrThis = $j(this).parents('tr:first');
			var jTrFull = jTrThis.next();
			jTrFull.hide();
			$j(this).removeClass("shrink").addClass("expand");
		});
	}
}	

//显示 隐藏详细
function toggleScreen(linkDOM){
	var jTrThis = $j(linkDOM).parents('tr:first');
	var consultId = jTrThis.attr("consultId");
	var jTrFull = jTrThis.next();
	if(jTrFull.is(":hidden")){
		jTrFull.show();
		$j(linkDOM).removeClass("expand").addClass("shrink");
	}else{
		jTrFull.hide();
		$j(linkDOM).removeClass("shrink").addClass("expand");
	}
	if($j('#readStatus_'+consultId).hasClass("unRead")){
		//未读状态
		markRead(consultId);
	}
	
}

//显示操作按钮
function displayListButtons(status){
}


//刷新产品列表
function refreshEntityList(){
	searchEntities(false);
}
//分页
function fnOnGoToPage(){
	refreshEntityList();
}

//对已有结果进行排序,ascValue表示升序时对应的值,descValue表示降序时对应的值
function sortField(linkDOM,ascValue,descValue){
	var jThDOM = $j(linkDOM).parent('th');
	if(jThDOM.hasClass("order1")){
		$j('#storageOrderby').val(descValue);
	}else{
		$j('#storageOrderby').val(ascValue);
	}
	searchEntities(false);
}

//搜索 isNew标示是否新的搜索,param标示是否自定义参数搜索
function searchEntities(isNew,param){
	var postData;
	var url;
	if(isNew){
		if(null==param){
			if(validateSearchCriteria()){
				//有搜索结果搜索
			    postData = $j('#entitySearchCriteria :input').serializeArray();
			    url=__ctxPath+"/customer/consult/blank.html?doAction=search&btnSearch=1";
				$j("#entityResults").loading().load(url,postData);
		    }else{
		    	return;
		    }
		}else{
			//新的搜索。全部，上架等
			if(param.handleStatus  != undefined){
				$j('#handleStatus').val(""+param.handleStatus);
			}
			//有搜索结果搜索
		    postData = $j('#entityResults :input').serializeArray();
		    url=__ctxPath+"/customer/consult/blank.html?doAction=search&btnSearch=1";
			$j("#entityResults").loading().load(url,postData);  
		}    
	}else{
		postData = $j('#entityResults :input').serializeArray();
		url=__ctxPath+"/customer/consult/blank.html?&doAction=search";
		$j("#entityResults").loading().load(url,postData);  
	} 
	
}



//验证函数
function validateSearchCriteria(){
	if(!validateForm($j('#entitySearchCriteria').get(0))){
    	return false;
   	}
   	return true;
}

//重设搜索条件
function resetSearchCriteria(){
	fnResetForm("entitySearchCriteria");
}


//标识为已阅/未阅
function multiToggleReadStatus(){
	var paraData=$j("#entityListForm :input[name='multiIds']").serializeArray();
	if(paraData.length>0){
		$j.post(__ctxPath + "/customer/consult/blank.html?doAction=multiToggleReadStatus", paraData, multiToggleReadStatusCallback);
	}else{
		alert(__FMT.consultList_selectConsult);
	}
}
//标识为已阅/未阅 callback
function multiToggleReadStatusCallback(){
	sysMsg(__FMT.consultList_message_multiToggleReadStatus_success);
	refreshEntityList();
}
//标识为已阅
function markRead(consultId){
	$j.post(__ctxPath + "/customer/consult/blank.html?doAction=markRead&consultId="+consultId, null, markReadCallback,"json");
}
//标识为已阅callback
function markReadCallback(data){
	$j('#readStatus_'+data.consultId).removeClass("unRead").addClass("read").attr("title",__FMT.consult_isRead_read)
}

//显示/隐藏
function multiToggleShowStatus(){
	var paraData=$j("#entityListForm :input[name='multiIds']").serializeArray();
	if(paraData.length>0){
		$j.post(__ctxPath + "/customer/consult/blank.html?doAction=multiToggleShowStatus", paraData, multiToggleShowStatusCallback);
	}else{
		alert(__FMT.consultList_selectConsult);
	}
}
//显示/隐藏 callback
function multiToggleShowStatusCallback(){
	sysMsg(__FMT.consultList_message_multiToggleShowStatus_success);
	refreshEntityList();
}

//删除
function multiDelete(){
	var paraData=$j("#entityListForm :input[name='multiIds']").serializeArray();
	if(paraData.length>0){
		var itemNames = fnGetSelectedItemNames();
		if(confirm( __FMT.common_message_confirmDeleteThis+"?")){
			$j.post(__ctxPath + "/customer/consult/blank.html?doAction=multiDelete", paraData, multiDeleteCallback);
		}	
	}else{
		alert(__FMT.consultList_selectConsult);
	}
}
//删除 callback
function multiDeleteCallback(){
	sysMsg(__FMT.consultList_message_multiDelete_success);
	refreshEntityList();
}


//管理员回复
function replyConsult(consultId){
	if($('message_'+consultId).value.trim()==""){
		jAlert(__FMT.consultList_message_messageCannotBeEmpty);
		return;
	}
	applyValidate($j('#message_'+consultId).get(0),'maxlength=1000');
	if(!validateForm($j("#replyDiv_"+consultId).get(0))){
		jAlert(__vaMsg.notPass);
		return;
	}
	var paraData=$j("#replyDiv_"+consultId+" :input").serializeArray();
	$j.post(__ctxPath + "/customer/consult/blank.html?doAction=replyConsult", paraData, replyConsultCallback,"json");
}
//管理员回复callback
function replyConsultCallback(data){
	jAlert(__FMT.consultList_message_replyConsult_success);
	$('message_'+data.consultId).value = "";
	refreshReplyCount(data.consultId);
}
//更新回复数量  关闭评论回复窗口（consultId 当前评论id） 新发表评论时候会调用
function refreshReplyCount(consultId){
	//更新回复数量
	$j.post(__ctxPath + "/customer/consult/blank.html?doAction=getConsult&consultId="+consultId, null, refreshReplyCountCallback,"json");
}
//更新回复数量   callback
function refreshReplyCountCallback(data){
	$j('#replyCount_'+data.consultId).html('('+data.onShowReplyCount+'/'+data.replyCount+')');
	
	var jHandle = $j('#handleStatus_'+data.consultId);
	if(data.onShowReplyCount>=1){
		jHandle.addClass("handle").removeClass("unHandle");
		jHandle.attr("title",__FMT.consult_isHandle_handle);
	}else{
		jHandle.addClass("unHandle").removeClass("handle");
		jHandle.attr("title",__FMT.consult_isHandle_unHandle);
	}
}
	